package com.zc.manager.notify.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import com.zc.manager.notify.util.ProviderUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.zc.manager.notify.domain.NotifyConfig;
import com.zc.manager.notify.service.NotifyConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通知配置Controller
 *
 * @author wangjiabao
 * @date 2022-11-10
 */
@RestController
@RequestMapping("/notify/config")
public class NotifyConfigController extends BaseController {

    @Resource
    private NotifyConfigService notifyConfigService;

    /**
     * 查询通知配置列表
     */
    @PreAuthorize("@ss.hasPermi('iot:network:list')")
    @CrossOrigin
    @GetMapping("/list")
    public TableDataInfo list(NotifyConfig notifyConfig) {
        startPage();
        List<NotifyConfig> list = notifyConfigService.selectNotifyConfigList(notifyConfig);
        return getDataTable(list);
    }

    /**
     * 查询通知配置列表
     */
    @PreAuthorize("@ss.hasPermi('iot:network:list')")
    @CrossOrigin
    @GetMapping("/query")
    public TableDataInfo query(NotifyConfig notifyConfig) {
        List<NotifyConfig> list = notifyConfigService.selectNotifyConfigList(notifyConfig);
        return getDataTable(list);
    }

    /**
     * 导出通知配置列表
     */
    @PreAuthorize("@ss.hasPermi('notify:config:export')")
    @Log(title = "通知配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NotifyConfig notifyConfig) {
        List<NotifyConfig> list = notifyConfigService.selectNotifyConfigList(notifyConfig);
        ExcelUtil<NotifyConfig> util = new ExcelUtil<>(NotifyConfig.class);
        util.exportExcel(response, list, "通知配置数据");
    }

    /**
     * 获取通知配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('notify:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(notifyConfigService.selectNotifyConfigById(id));
    }

    /**
     * 新增通知配置
     */
    @PreAuthorize("@ss.hasPermi('notify:config:add')")
    @Log(title = "通知配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody NotifyConfig notifyConfig) {
        return toAjax(notifyConfigService.insertNotifyConfig(notifyConfig));
    }

    /**
     * 修改通知配置
     */
    @PreAuthorize("@ss.hasPermi('notify:config:edit')")
    @Log(title = "通知配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NotifyConfig notifyConfig) {
        return toAjax(notifyConfigService.updateNotifyConfig(notifyConfig));
    }

    /**
     * 删除通知配置
     */
    @PreAuthorize("@ss.hasPermi('notify:config:remove')")
    @Log(title = "通知配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(notifyConfigService.deleteNotifyConfigByIds(ids));
    }

    /**
     * 根据通知类型获取服务商
     */
    @PreAuthorize("@ss.hasPermi('notify:config:query')")
    @GetMapping("/type")
    public AjaxResult getProvider(NotifyConfig notifyConfig) {

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map = new HashMap<>();

        map.put("value", ProviderUtil.getValue(notifyConfig.getType()));
        map.put("name", ProviderUtil.getName(notifyConfig.getType()));
        list.add(0, map);

        return AjaxResult.success(list);

    }

    @Log(title = "配置导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('notify:config:import')")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<NotifyConfig> util = new ExcelUtil<NotifyConfig>(NotifyConfig.class);
        List<NotifyConfig> notifyConfigList = util.importExcel(file.getInputStream());
        return toAjax(notifyConfigService.importNotifyConfig(notifyConfigList));
    }

}
